#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Python版Delphi IDE核心应用类"""

from PyQt5.QtWidgets import (
    QMainWindow, QDockWidget, QTabWidget, QSplitter, 
    QWidget, QVBoxLayout, QMenuBar, QMenu, QAction,
    QToolBar, QStatusBar
)
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QIcon, QFont

from src.ui.editor import CodeEditor
from src.ui.designer import UIDesigner
from src.ui.property_editor import PropertyEditor
from src.ui.object_inspector import ObjectInspector
from src.ui.project_explorer import ProjectExplorer


class DelphiLikeIDE(QMainWindow):
    """Delphi风格的IDE主窗口类"""
    
    def __init__(self):
        """初始化IDE主窗口"""
        super().__init__()
        
        # 设置窗口基本属性
        self.setWindowTitle("Python Delphi-like IDE")
        self.resize(1200, 800)
        
        # 初始化UI组件
        self.init_ui()
        self.init_menus()
        self.init_toolbars()
        self.init_status_bar()
        
        # 初始化停靠窗口
        self.init_docks()
        
    def init_ui(self):
        """初始化主界面"""
        # 创建主分割器
        main_splitter = QSplitter(Qt.Horizontal)
        
        # 创建中心区域的标签页
        self.central_tabs = QTabWidget()
        
        # 添加代码编辑器标签
        self.code_editor = CodeEditor()
        self.central_tabs.addTab(self.code_editor, "编辑器")
        
        # 添加UI设计器标签
        self.ui_designer = UIDesigner()
        self.central_tabs.addTab(self.ui_designer, "设计器")
        
        # 添加到主分割器
        main_splitter.addWidget(self.central_tabs)
        
        # 设置中心部件
        self.setCentralWidget(main_splitter)
    
    def init_menus(self):
        """初始化菜单栏"""
        # 获取菜单栏
        menubar = self.menuBar()
        
        # 文件菜单
        file_menu = menubar.addMenu("文件")
        
        new_action = QAction("新建", self)
        new_action.setShortcut("Ctrl+N")
        file_menu.addAction(new_action)
        
        open_action = QAction("打开", self)
        open_action.setShortcut("Ctrl+O")
        file_menu.addAction(open_action)
        
        save_action = QAction("保存", self)
        save_action.setShortcut("Ctrl+S")
        file_menu.addAction(save_action)
        
        file_menu.addSeparator()
        
        exit_action = QAction("退出", self)
        exit_action.setShortcut("Ctrl+Q")
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)
        
        # 编辑菜单
        edit_menu = menubar.addMenu("编辑")
        
        # 查看菜单
        view_menu = menubar.addMenu("查看")
        
        # 工具菜单
        tools_menu = menubar.addMenu("工具")
        
        # 帮助菜单
        help_menu = menubar.addMenu("帮助")
    
    def init_toolbars(self):
        """初始化工具栏"""
        # 主工具栏
        main_toolbar = QToolBar("Main Toolbar", self)
        main_toolbar.setIconSize(QSize(16, 16))
        self.addToolBar(main_toolbar)
        
        # 添加工具栏按钮
        new_action = QAction("新建", self)
        main_toolbar.addAction(new_action)
        
        open_action = QAction("打开", self)
        main_toolbar.addAction(open_action)
        
        save_action = QAction("保存", self)
        main_toolbar.addAction(save_action)
        
        main_toolbar.addSeparator()
        
        run_action = QAction("运行", self)
        main_toolbar.addAction(run_action)
        
        debug_action = QAction("调试", self)
        main_toolbar.addAction(debug_action)
    
    def init_status_bar(self):
        """初始化状态栏"""
        status_bar = QStatusBar()
        self.setStatusBar(status_bar)
        status_bar.showMessage("就绪")
    
    def init_docks(self):
        """初始化停靠窗口"""
        # 项目浏览器
        self.project_explorer = ProjectExplorer()
        project_dock = QDockWidget("项目", self)
        project_dock.setWidget(self.project_explorer)
        project_dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
        self.addDockWidget(Qt.LeftDockWidgetArea, project_dock)
        
        # 对象检查器
        self.object_inspector = ObjectInspector()
        object_dock = QDockWidget("对象", self)
        object_dock.setWidget(self.object_inspector)
        object_dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
        self.addDockWidget(Qt.RightDockWidgetArea, object_dock)
        
        # 属性编辑器
        self.property_editor = PropertyEditor()
        property_dock = QDockWidget("属性", self)
        property_dock.setWidget(self.property_editor)
        property_dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
        self.tabifyDockWidget(object_dock, property_dock)